<script language="javascript">
/*™*******************************************************************************************™*/

$().ready(function() {

	$("select").select2({
		placeholder: "กรุณาเลือกข้อมูล",
		width: 250
	});

    $("#BTTdeldoc").click(function () {

		var handel = confirm("คุณแน่ใจที่จะลบเอกสารเบอร์นี้ใช่หรือไม่ ?");
		if (handel == true) {
		  return true;
		} else {
		  return false;
		}

	});

	// check numeric
	$(".numeric").numeric();

	// slide panel
	//$(".btn-slide").click(function(){
		$("#panel").slideToggle("fast");
		$(this).toggleClass("active");
		//return false;
	//});

	//scroll the message box to the top offset of browser's scrool bar
	$(window).scroll(function() {
	  //$("#mainPanel").animate({top: $(window).scrollTop() + "px" }, {queue: false, duration: 1000});
	});

	// autocomplete
	$("#TXTdescription_0").autocomplete("./gl_account_list.php?ty=ar", {
		width: 250,
		selectFirst: false,
		formatItem: function formatItem(row) {
			return row[0];
			//return row[0] + " : " + row[1];
		},
		formatMatch: function formatMatch(row) {
			//
		},
		formatResult: function formatResult(row) {
			return row[0];
			//return row[0].replace(/(<.+?>)/gi, "");
		}

	});

});

/*™*******************************************************************************************™*/

$().ready(function() {

	$("#BTTpost").hide();

	$("input:text, select").livequery("focus", function() {
		$(this).addClass("input_focus");
	});

	$("input:text, select").livequery("blur", function() {
		$(this).removeClass("input_focus");
	});

});

/*™*******************************************************************************************™*/

$().ready(function() {

    $('#TXTdoc_date_0').datepicker({ dateFormat: 'yy-mm-dd', speed: 'fast', clickInput:true });

});

/*™*******************************************************************************************™*/

$().ready(function(){

    $("#BTTnewrow").livequery("click", function() {

		var num_row = $("#HIDnum_row").val();

		<?php
			$sqlCommand = "SELECT budget_name, budget_code FROM tbl_budget ORDER BY budget_code";
			$recordset = $objConn->Execute($sqlCommand);

			$dropdownlistBUDGET = eregi_replace("\n", "", $recordset->GetMenu("SELbudget[]", "", true, false, 0, "class='input'"));
			$dropdownlistBUDGET = eregi_replace("\r", "", $dropdownlistBUDGET);
			$dropdownlistBUDGET = eregi_replace('"', "'", $dropdownlistBUDGET);

			$sqlCommand = "(SELECT payer_name, payer_code FROM tbl_customer ORDER BY payer_id) UNION (SELECT payer_name, payer_code FROM tbl_vendor ORDER BY payer_id)";
			$recordset = $objConn->Execute($sqlCommand);

			$dropdownlistPAYER = eregi_replace("\n", "", $recordset->GetMenu("SELpayer[]", "", true, false, 0, "class='input'"));
			$dropdownlistPAYER = eregi_replace("\r", "", $dropdownlistPAYER);
			$dropdownlistPAYER = eregi_replace('"', "'", $dropdownlistPAYER);
		?>

		num_row = parseFloat(num_row) + parseFloat(1);
		$("#HIDnum_row").val(num_row);

		$("<tr id=rowNo_" + num_row + "><td><input name='TXTdescription[]' type='text' class='input' id='TXTdescription_" + num_row + "' size='35' /></td><td><?php echo $dropdownlistBUDGET; ?></td><td><?php echo $dropdownlistPAYER; ?></td><td><input name='TXTremark[]' type='text' class='input' id='TXTremark_" + num_row + "' size='35' /></td><td><input name='TXTquantity[]' type='text' class='numeric' id='TXTquantity_" + num_row + "' size='5' /></td><td><input name='TXTprice_unit[]' type='text' class='numeric' id='TXTprice_unit_" + num_row + "' size='5' /></td><td bgcolor='#FFCC99'><input name='TXTcash[]' type='text' class='numeric' id='TXTcash_" + num_row + "' size='10' /></td><td bgcolor='#FFCC99'><input name='TXTsaving[]' type='text' class='numeric' id='TXTsaving_" + num_row + "' size='10' /></td><td bgcolor='#FFCC99'><input name='TXTcurrent[]' type='text' class='numeric' id='TXTcurrent_" + num_row + "' size='10' /></td><td bgcolor='#FFCC99'><input name='TXTpledge[]' type='text' class='numeric' id='TXTpledge_" + num_row + "' size='10' /></td></tr>").insertBefore("#addNewRow");

		// check numeric
		$(".numeric").numeric();

		// autocomplete
		$("#TXTdescription_" + num_row).autocomplete("./gl_account_list.php?ty=ar", {
			width: 250,
			selectFirst: false,
			formatItem: function formatItem(row) {
				return row[0];
				//return row[0] + " : " + row[1];
			},
			formatMatch: function formatMatch(row) {
				//
			},
			formatResult: function formatResult(row) {
				return row[0];
				//return row[0].replace(/(<.+?>)/gi, "");
			}
		});

		$("select").select2({
			placeholder: "กรุณาเลือกข้อมูล",
			width: 250
		});

	});

});

/*™*******************************************************************************************™*/

$().ready(function(){

    $("#BTTremoverow").click(function () {

		var num_row = $("#HIDnum_row").val();

		if (num_row != "0") { // prevent delete first line
			$("#rowNo_" + num_row).remove(); // remove element

			num_row = parseFloat(num_row) - parseFloat(1);
			$("#HIDnum_row").val(num_row);
		}

	});

});

/*™*******************************************************************************************™*/

$().ready(function(){

    $("#BTTnewrow").click(function () {
		$("#BTTpost").hide();
	});

    $("#BTTremoverow").click(function () {
		$("#BTTpost").hide();
	});

});

/*™*******************************************************************************************™*/

$().ready(function() {

	/* submit form */
	var options = {
		dataType: "json",
		beforeSubmit: showRequest,
		success: showResponse
	}

	$("form").ajaxForm(options);

	return false;  // always return false to prevent std. browser submit and page navigation

});

/*™*******************************************************************************************™*/

function showRequest(formData, jqForm, options) {

	/* blockUI plugin */
	jQuery.blockUI({
		message: "<div id='divLoading'>Please wait ...</div>",
		css: {
			border: "none",
			padding: "15px",
			backgroundColor: "#000000", "-webkit-border-radius": "10px", "-moz-border-radius": "10px",
			opacity: "0.5",
			color: "#ffffff"
		}
	});

}

/*™*******************************************************************************************™*/

function showResponse(responseText, statusText) {

	jQuery.unblockUI();

	$.each(responseText, function(key, value) { // json format
		if (key == "POSTING_SUCCESS") {
			alert(value);
		} else if (key == "POSTING_REDIRECT") {
			$("#BTTpost").hide();
			window.location.href = unescape(value);
		} else if (key == "SIMULATE_SUCCESS") {
			alert(value);
			$("#BTTpost").show();
		} else if (key == "SIMULATE_FAIL") {
			alert(value);
			$("#BTTpost").hide();
		} else if (key == "TXTtotal_amount_cash") {
			$("#TXTtotal_amount_cash").val(value);
		} else if (key == "TXTtotal_amount_saving") {
			$("#TXTtotal_amount_saving").val(value);
		} else if (key == "TXTtotal_amount_current") {
			$("#TXTtotal_amount_current").val(value);
		} else if (key == "TXTtotal_amount_pledge") {
			$("#TXTtotal_amount_pledge").val(value);
		} else if (key.substring(0, 8) == "TXTcash_") {
			$("#" + key).val(value);
		} else {
			alert(value);
		}
	});

}

/*™*******************************************************************************************™*/
</script>
